package com.canbot.u05.activity.customvoice.tool

import android.graphics.Rect
import android.support.v7.widget.RecyclerView
import android.view.View

/**
 * GridSpacingItemDecoration
 *
 * @author Liu.heng
 * @date 2018/8/27.
 */

class GridSpacingItemDecoration(
        /**列数*/
        private val spanCount: Int,
        /**间隔*/
        private val spacing: Int,
        /**是否包含边缘*/
        private val includeEdge: Boolean) : RecyclerView.ItemDecoration() {

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {

        /**这里是关键，需要根据你有几列来判断 */
        val position = parent.getChildAdapterPosition(view)
        val column = position % spanCount

        if (includeEdge) {
            outRect.left = spacing - column * spacing / spanCount
            outRect.right = (column + 1) * spacing / spanCount

            if (position < spanCount) {
                outRect.top = spacing
            }
            outRect.bottom = spacing
        } else {
            outRect.left = column * spacing / spanCount
            outRect.right = spacing - (column + 1) * spacing / spanCount
            if (position >= spanCount) {
                /**item top */
                outRect.top = spacing
            }
        }
    }
}
